/*******************************************************************
 * author   :  Charlies-ls
 * Date     :  2021-09-26
 * Copyright (c) 2021. artestplay@outlook.com. All Rights Reserved.
 *******************************************************************/
#include "ax_fifo.h"
#include "com.h"
#include "ax_printf.h"

ax_fifo *ax_fifo_create()
{
    ax_fifo* fifo = ALLOC(ax_fifo);
    fifo->l = ax_list_create();
    return fifo;
}

void ax_fifo_destory(ax_fifo *fifo)
{
    ax_check_and_return(fifo, "empty");
    ax_list_destory(fifo->l);
    FREE(fifo);
}

void ax_fifo_push(ax_fifo *fifo, void *data)
{
    ax_check_and_return(fifo && data, "error");
    ax_list_push_back_data(fifo->l, data);
}

void* ax_fifo_pop(ax_fifo *fifo)
{
    ax_check_and_return_zero(fifo, "empty");
    ax_list_node* node = ax_list_del_front(fifo->l);
    void*         data = node->data;
    FREE(node);
    return data;
}

void *ax_fifo_front(ax_fifo *fifo)
{
    ax_check_and_return_zero(fifo, "empty");
    ax_list_node* node = ax_list_front(fifo->l);
    ax_check_and_return_zero(node, "empty");
    return node->data;
}

void *ax_fifo_back(ax_fifo *fifo)
{
    ax_check_and_return_zero(fifo, "empty");
    ax_list_node* node = ax_list_back(fifo->l);
    ax_check_and_return_zero(node, "empty");
    return node->data;
}

int ax_fifo_size(ax_fifo *fifo)
{
    ax_check_and_return_zero(fifo, "empty");
    return ax_list_size(fifo->l);

}
